import os

def find_large_files_and_folders(directory, size_limit):
    # 遍历目录
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_path = os.path.join(root, file)
            file_size = os.path.getsize(file_path)

            # 检查文件大小是否超过限制
            if file_size > size_limit:
                print(f"Large file found: {file_path} ({file_size} bytes)")

        for folder in dirs:
            folder_path = os.path.join(root, folder)
            folder_size = get_folder_size(folder_path)

            # 检查文件夹大小是否超过限制
            if folder_size > size_limit:
                print(f"Large folder found: {folder_path} ({folder_size} bytes)")

def get_folder_size(folder):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(folder):
        for filename in filenames:
            filepath = os.path.join(dirpath, filename)
            total_size += os.path.getsize(filepath)
    return total_size

# 指定目录和大小限制（以字节为单位）
directory_to_search = r'C:\Windows'
size_limit_bytes = 100 * 1024 * 1024  # 100MB

# 调用函数查找大文件和文件夹
find_large_files_and_folders(directory_to_search, size_limit_bytes)
